package reflection;

/**
 * @author 小小唐
 * @Date 2022/8/24-星期三-9:02
 *
 * 反射
 */
public class Demo1 {
    public static void main(String[] args) throws ClassNotFoundException {

        //通过反射获取类的class对象
        //注意一个类只有一个class对象
        Class c1 = Class.forName("reflection.User");

        System.out.println(c1);
        System.out.println(c1.hashCode());

        //一个类在内存中只有一个class对象
        //一个类被加载后，整个结构都会被封装在class对象中
        //通过hashCode可以比较是否为同一个类，hashCode值相等，则为同一个对象或是同一个类
        Class c2 = Class.forName("reflection.User");
        Class c3 = Class.forName("reflection.User");
        Class c4 = Class.forName("reflection.User");

        System.out.println(c1.hashCode()+"\t"+c2.hashCode()+"\t"+c3.hashCode()+"\t"+c4.hashCode());
    }
}

/**
 * 实体类:pojo  entity
 */
class User{

    private String name;
    private int id;
    private int age;


    //无参构造
    public User() {
    }

    //有参构造
    public User(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private void test(){}

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }
}
